package com.tal.pad.eye_protection.util;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * @anthor Mr.Zan
 * @email  644962006@qq.com
 * @time 2025/3/27 16:26
 * @describe  模型拷贝工具类
 */
public class ModelFileUtils {
    private static void copyFile(InputStream in, File target) {
        if (target != null) {
            boolean copySuccess = true;
            OutputStream out = null;

            try {
                out = new FileOutputStream(target);
                byte[] buffer = new byte[1024];

                int counter;
                while((counter = in.read(buffer)) != -1) {
                    out.write(buffer, 0, counter);
                }
            } catch (IOException e) {
                e.printStackTrace();
                copySuccess = false;
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }

                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (!copySuccess) {
                target.delete();
            }

        }
    }
    public static void copyModel(Context context, String modelPath, String modelName) {
//        if (!isModelExists(context, modelPath, modelName)) {
            File modelDir = new File(modelPath);
            if (!modelDir.exists()) {
                modelDir.mkdirs();
            }

            AssetManager assetManager = context.getAssets();

            try {
                String[] children = assetManager.list(modelName);

                for(int i = 0; i < children.length; i++) {
                    String child = children[i];
                    File childFile = new File(modelPath + "/" + child);
                    child = modelName + "/" + child;
                    if (!childFile.exists() || (long)assetManager.open(child).available() != childFile.length()) {
                        copyFile(assetManager.open(child), childFile);
                    }
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
//        }

    }
    private static boolean isModelExists(Context context, String modelPath, String modelName) {
        AssetManager assetManager = context.getAssets();

        try {
            String[] children = assetManager.list(modelName);

            for(int i = 0; i < children.length; i++) {
                String child = children[i];
                File childFile = new File(modelPath + "/" + child);
                child = modelName + "/" + child;
                if (!childFile.exists() || (long)assetManager.open(child).available() != childFile.length()) {
                    return false;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

}
